package net.lab1024.sa.base.common.elk;

import com.alibaba.fastjson.JSON;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.time.Instant;
import java.util.HashMap;
import java.util.Map;

@Component
public class ELKLogger {

    private final Logger logger;

    public ELKLogger() {
        this.logger = LoggerFactory.getLogger("ELK_LOGGER");
    }

    public void logApiCall(String apiPath, String method, Map<String, Object> params,
                           Object result, long executionTime, String userId) {
        Map<String, Object> logData = new HashMap<>();
        logData.put("type", "API_CALL");
        logData.put("apiPath", apiPath);
        logData.put("httpMethod", method);
        logData.put("params", params);
        logData.put("executionTime", executionTime);
        logData.put("userId", userId);
        logData.put("timestamp", Instant.now().toString());

        if (result != null) {
            logData.put("result", result);
        }

        logger.info(JSON.toJSONString(logData));
    }

    public void logBusinessEvent(String eventType, String eventName,
                                 Map<String, Object> data, String userId) {
        Map<String, Object> logData = new HashMap<>();
        logData.put("type", "BUSINESS_EVENT");
        logData.put("eventType", eventType);
        logData.put("eventName", eventName);
        logData.put("data", data);
        logData.put("userId", userId);
        logData.put("timestamp", Instant.now().toString());

        logger.info(JSON.toJSONString(logData));
    }
}